﻿function WriteXml([System.Xml.XmlDocument]$xml, [string]$file)
{
    $encoding = New-Object System.Text.UTF8Encoding($true)
    $writer = New-Object System.IO.StreamWriter($file, $false, $encoding)
    $xml.Save($writer)
    $writer.Close()
}

function SetVersion()
{    
    $file = "$($rootPath)\templates\MicroCloud.Templates.Frontend.nuspec"
    Write-Host ("正在更新文件 $($file) 的版本号：$($version)")
    $xml = New-Object -TypeName XML
    $xml.Load($file)
    $xml.package.metadata.version = $version
    WriteXml $xml $file
    Write-Host "$($file) 版本号更新成功：$($version)"
}

function GetVersion()
{
    $file = "$($rootPath)\template.props"
    $xml = New-Object -TypeName XML
    $xml.Load($file)
    $version = $xml.Project.PropertyGroup.Version
    if($version.contains("VersionSuffixVersion"))
    {
        $version = "{0}.{1}.{2}.{3}" -f $xml.Project.PropertyGroup.VersionMain,$xml.Project.PropertyGroup.VersionPrefix,$xml.Project.PropertyGroup.VersionSuffix,$xml.Project.PropertyGroup.VersionSuffixVersion
    }
    else
    {
        $version = "{0}.{1}" -f $xml.Project.PropertyGroup.VersionMain,$xml.Project.PropertyGroup.VersionPrefix
    }
    return $version
}

function BuildNugetPackages()
{
	$packages = "$($rootPath)\packages"
	if(Test-Path $packages)
    {
        Remove-Item ("$($packages)\*.*")
        Write-Host ("清空文件夹：$($packages)")
    }
    else
    {
        New-Item -ItemType directory -Path $packages
        Write-Host "创建文件夹：$($packages)"
    }
		 
	 $nuspec= ("templates/MicroCloud.Templates.Frontend.nuspec")	
	 .\nuget.exe pack $nuspec -OutputDirectory $packages -NoDefaultExcludes	              
}


function PushNugetPackages()
{
    $packages = "$($rootPath)\packages"
    if(!(Test-Path $packages))
    {
        Write-Host "输出文件夹 $($packages) 不存在"
        exit    
    }
    Write-Host "正在查找 nupkg 发布包,$($version)"
    $files = [System.IO.Directory]::GetFiles($packages, "*.$($version)*nupkg")
    Write-Host "共找到 $($files.Length) 个版本号为 $($version) 的nuget文件"
    if($files.Length -eq 0)
    {
        exit
    }
    
    $key = "$($rootPath)\nuget-apikey.txt"
    $key = [System.IO.File]::ReadAllText($key)
    $server = "https://api.nuget.org/v3/index.json"
    Write-Host "nuget服务器：$($server)，密钥：$($key)"
    $items=@()
    foreach($file in $files)
    {
        $obj = New-Object PSObject -Property @{
            Server = $server
            File = $file
            Key = $key
        }
        $items += @($obj)
    }

    $items | ForEach-Object {        		
        $item = $_
        $name = [System.IO.Path]::GetFileName($item.File)
        Write-Host ("正在 {0} 向发布{1}" -f $item.Server, $name)		 		
		dotnet nuget push $item.File --api-key $item.Key --source $item.Server			
    }
}

$now = [DateTime]::Now
$rootPath = ($ENV:WORKSPACE)
if($rootPath -eq $null)
{
    $rootPath = Split-Path -Parent $MyInvocation.MyCommand.Definition    
}
Write-Host ("当前目录：$($rootPath)")
$version = GetVersion
Write-Host ("当前版本：$($version)")
SetVersion
BuildNugetPackages
PushNugetPackages
